/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


 ************************************************************************************************
 Objet ABSTRACTCONTAINER
 VERSION:0.85
 DATE:15/06/2006
 ************************************************************************************************
Init parameter in objPar (object of parameter):

//extend rialto.widget.AbstractComponent
//= super class of all container's class
//      property : table of components as children   (.arrChild) 
//      method for add component in container        (.add)
//      method for remove component from container   (.removeContents)
//      method for record to make link between container and componnent  (.record)
*************************************************************************************************/

rialto.widget.AbstractContainer=function (objPar){
//================================
    //base
    this.base = rialto.widget.AbstractComponent;    
    this.base(objPar);
	this.arrChild = new Array;
    //table of container's child components (rialto widgets (or not?))
    this.isContainer=true;
    }

rialto.widget.AbstractContainer.prototype = new rialto.widget.AbstractComponent;
//=========================

rialto.widget.AbstractContainer.prototype.add = function(elt,callByElt){    
//=============================  
	// add a component to the container 
    if (!elt) return;
    
    var comp, oHtml;
    //-- Rialto-component
    if (elt.bRiaComp) {
    	comp  = elt;
    	oHtml = elt.getHtmlExt(); 
    }	
    //-- obj Html element of Rialto-component
    else
    if (elt.oRia || elt.oCiu) {
    	comp  = elt.oRia || elt.oCiu;
    	oHtml = elt;
    }
    //-- obj Html independent
    else {    
    	comp = oHtml = elt;
    }
    
    // Html layout
    this.getHtmlCont().appendChild(oHtml);
	this.record(comp);

		    
	//This method is to call after the component is add to the document
    if (!callByElt && comp.afterPlaceIn) {
    	comp.afterPlaceIn();
    }
    
    	
}
rialto.widget.AbstractContainer.prototype.record = function (oRia) {
//================================
// record (in container) references to elements in container
	if (!this.arrChild) 
		this.arrChild = new Array;		
	this.arrChild.push(oRia);
    oRia.inContainer = this;	// reverse link
}

rialto.widget.AbstractContainer.prototype.removeContents = function(elt) {
//===========================================
// remove a container's component from container

	var comp;
    //-- Rialto-component
    if (elt.bRiaComp) 
    	comp = elt;
    //-- obj Html element of Rialto-component
    else if (elt.oRia) 
    	comp = elt.oRia;
    else
    //-- obj Html independent
    	comp = elt;
    	
    rialto.array.remove(this.arrChild,comp);    
    this.updateToContent();
}   
 
rialto.widget.AbstractContainer.prototype.baseSetVisible = rialto.widget.AbstractComponent.prototype.setVisible;
rialto.widget.AbstractContainer.prototype.setVisible= function(visible){
	this.baseSetVisible(visible);
	if(visible){this.$$activeContent();}	
}
 
rialto.widget.AbstractContainer.prototype.updateToContent = function(){
//=========================================
// update container according to the modification of the contents
}

//To adapt child component after a activation for example
rialto.widget.AbstractContainer.prototype.$$activeContent = function () {
	if (!this.arrChild) return;
    for(var i=0; i < this.arrChild.length; i++) {  
    	child = this.arrChild[i];
 		if (this.arrChild[i].adaptAfterContainerChange)
 			this.arrChild[i].adaptAfterContainerChange();
     }	
}

rialto.widget.AbstractContainer.prototype.adaptAfterContainerChange = function () {
	this.$$activeContent();
}

rialto.widget.AbstractContainer.prototype.getHtmlCont = function(){
//=====================================   
 //must return the HTML container element
 return this.divExt;
}

//REMOVE FROM PARENT
//parameter bFromContainer:true if the remove is cause by the container remove
rialto.widget.AbstractContainer.prototype.remove = function(bFromContainer){          
//================================         

	//release specific references
	this.release();
	//call child remove
	for(var i=0;i<this.arrChild.length;i++){
		if(this.arrChild[i].remove){
		        this.arrChild[i].remove(true);
	    }
	}
	this.arrChild=null;
	// remove Html elts
    var oHtml=this.getHtmlExt();
    if(oHtml.parentNode){oHtml.parentNode.removeChild(oHtml);}    

	// inform the container of remove (if the remove is not cause by the container remove)
	if(!bFromContainer){
		if (this.inContainer && this.inContainer.removeContents)
			this.inContainer.removeContents(this);
	}
		    
     //remove global reference                          	 
     rialto.session.objects[this.id]=null;
     this.inParent=null;
     oHtml.oCiu=null;
     oHtml.oRia=null;
     for (prop in this){this[prop]=null};
}
/*********************GESTION DU RESIZE *********************/
// to revise :

rialto.widget.AbstractContainer.prototype.resizeChilds=function(autoW,autoH){                  
    for(var i=0;i<this.arrChild.length;i++){            
	    if(!autoW && ! autoH){
		    if((this.arrChild[i].autoResizableW || this.arrChild[i].autoResizableH) && this.arrChild[i].updateSize ){
		        //alert("updateSize de "+this.arrChild[i].id);
		        this.arrChild[i].updateSize();
	        }
	    }
		else{	    
		    if(autoW && this.arrChild[i].autoResizableW &&  this.arrChild[i].updateWidth){
			    //alert("updateWidth de "+this.arrChild[i].id);
		    	this.arrChild[i].updateWidth();
		    }
		    if(autoH && this.arrChild[i].autoResizableH &&  this.arrChild[i].updateHeight){
				//alert("updateHeight de "+this.arrChild[i].id);
		    	this.arrChild[i].updateHeight();	    
		    }	    
		} 
    }

}

